package mat.xls;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Number;
import seotools.positions.direct.DirectAd;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

public class ExcelHelper {
//    public static int count = 1;

    public static void main(String[] args) throws IOException, WriteException, BiffException {
//        String file = "test.xls";
//        File xlsFile = new File(file);
//        WorkbookSettings s = new WorkbookSettings();
//        s.setLocale(new Locale("ru", "RU"));
//        WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, s);
//
//        workbook.createSheet("Тексты", 0);
//
//        WritableSheet sheet = workbook.getSheet(0);
//        addDirectHeader(sheet);
//        DirectAd ad = new DirectAd();
//        ad.header = "Nokia 8800";
//        ad.text = "Buy Nokia 8800";
//        ad.maxPrice = 0.2;
//        ad.adCount = 2;
//        ad.url = "http://extramobi.ru/product/accumulator_nokia_bl-4s/";
//        addAd(ad, sheet);
//
//        workbook.write();
//        workbook.close();

//        Workbook workbook = Workbook.getWorkbook(new File("C:\\projects\\seotools\\data\\orders\\balance.xls"));
//        Sheet sheet = workbook.getSheet(0);
//        for (int j = 7; j < Integer.MAX_VALUE; j++) {
//            try {
//                String[] fields = sheet.getCell(0, j).getContents().split(",");
//                String name = "";
//                System.out.println(name);
//            } catch (ArrayIndexOutOfBoundsException e) {
//                break;
//            }
//        }
    }

    public static void addDirectHeader(WritableSheet sheet) throws WriteException {
        WritableCell cell = new Label(0, 1, "Номер объявления");
        sheet.addCell(cell);
        cell = new Label(1, 1, "ID фразы");
        sheet.addCell(cell);
        cell = new Label(2, 1, "Фраза (с минус-словами)");
        sheet.addCell(cell);
        cell = new Label(3, 1, "ID объявления");
        sheet.addCell(cell);
        cell = new Label(4, 1, "Заголовок");
        sheet.addCell(cell);
        cell = new Label(5, 1, "Текст");
        sheet.addCell(cell);
        cell = new Label(6, 1, "заголовок");
        sheet.addCell(cell);
        sheet.mergeCells(6, 0, 7, 0);
        cell = new Label(6, 0, "Длина");
        sheet.addCell(cell);
        cell = new Label(7, 1, "текст");
        sheet.addCell(cell);
        cell = new Label(8, 1, "Ссылка");
        sheet.addCell(cell);
        cell = new Label(9, 1, "Регион");
        sheet.addCell(cell);
        cell = new Label(10, 1, "Ставка");
        sheet.addCell(cell);
        cell = new Label(11, 1, "Контакты");
        sheet.addCell(cell);
        cell = new Label(12, 1, "Статус объявления");
        sheet.addCell(cell);
        cell = new Label(13, 1, "Статус фразы");
        sheet.addCell(cell);
        cell = new Label(14, 1, "Заголовки дополнительных ссылок");
        sheet.addCell(cell);
        cell = new Label(15, 1, "Адреса дополнительных ссылок");
        sheet.addCell(cell);
        cell = new Label(16, 1, "Параметр 1");
        sheet.addCell(cell);
        cell = new Label(17, 1, "Параметр 2");
        sheet.addCell(cell);
        cell = new Label(18, 1, "Метки");
        sheet.addCell(cell);
    }

    public static void addAd(DirectAd ad, WritableSheet sheet, int count) throws WriteException {
        WritableCell cell = new Number(0, count, ad.count);
        sheet.addCell(cell);
        if (ad.adId == 0) {
            cell = new Label(0, count, "");
        } else {
            cell = new Number(0, count, ad.adId);
        }
        sheet.addCell(cell);
        if (ad.phraseId == 0) {
            cell = new Label(1, count, "");
        } else {
            cell = new Number(1, count, ad.phraseId);
        }
        sheet.addCell(cell);
        cell = new Label(2, count, ad.phrase);
        sheet.addCell(cell);
        cell = new Label(4, count, ad.header);
        sheet.addCell(cell);
        cell = new Label(5, count, ad.text);
        sheet.addCell(cell);
        cell = new Number(6, count, ad.getHeaderLength());
        sheet.addCell(cell);
        cell = new Number(7, count, ad.getTextLength());
        sheet.addCell(cell);
        cell = new Label(8, count, ad.url);
        sheet.addCell(cell);
        cell = new Label(9, count, ad.region);
        sheet.addCell(cell);
        cell = new Number(10, count, ad.maxPrice);
        sheet.addCell(cell);
        cell = new Label(11, count, ad.contacts);
        sheet.addCell(cell);
        cell = new Label(12, count, ad.adStatus);
        sheet.addCell(cell);
        cell = new Label(13, count, ad.phraseStatus);
        sheet.addCell(cell);
        cell = new Label(14, count, ad.addLinksHeader);
        sheet.addCell(cell);
        cell = new Label(15, count, ad.addLinksAddress);
        sheet.addCell(cell);
    }
}
